<template>
  <div class="child2">
    <h3>子组件2</h3>
    <h4>电脑：{{ computer }}</h4>
    <h3 v-if="toy">哥哥给的玩具:{{ toy }}</h3>
  </div>
</template>

<script setup lang="ts" name="Child2">
import { onUnmounted, ref } from "vue";
import emitter from "../../utils/emitter";
let computer = ref("联想");
let toy = ref("");

// 给emitter绑定send-toy事件
emitter.on("send-toy", (val: any) => {
  toy.value = val;
});

// 组件销毁的时候，去解绑事件，不然对内存不友好
onUnmounted(() => {
  emitter.off("send-toy");
});
</script>

<style scoped>
.child2 {
  margin-top: 50px;
  background-color: orange;
  padding: 10px;
  box-shadow: 0 0 10px black;
  border-radius: 10px;
}
</style>
